/*
 * Copyright (C) 2013 AndXmppSDK Project <hljdrl@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.drl.xmpp;

import java.util.HashMap;
import java.util.Map;

import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Mode;

import android.annotation.SuppressLint;


/**
 * 
 * 用户和自己的登录状态-各种登录、离开、忙碌的状态对象类
 * 
 * @author hljdrl@gmail.com
 *
 */
public final class PresenceStatus {

	
	/** Status of a disconnected contact. */
    public static final int CONTACT_STATUS_DISCONNECT = 100;

    /** Status of a unavailable (long away) contact. */
    public static final int CONTACT_STATUS_UNAVAILABLE = 200;

    /** Status of a away contact. */
    public static final int CONTACT_STATUS_AWAY = 300;

    /** Status of a busy contact. */
    public static final int CONTACT_STATUS_BUSY = 400;

    /** Status of a available contact. */
    public static final int CONTACT_STATUS_AVAILABLE = 500;

    /** Status of a available for chat contact. */
    public static final int CONTACT_STATUS_AVAILABLE_FOR_CHAT = 600;


    /**
     * Get the smack presence mode for a status.
     * @param status the status in beem
     * @return the presence mode to use in presence packet or null if there is no mode to use
     */
    public static Presence.Mode getPresenceModeFromStatus(final int status) {
	Presence.Mode res;
	switch (status) {
	    case CONTACT_STATUS_AVAILABLE:
		res = Presence.Mode.available;
		break;
	    case CONTACT_STATUS_AVAILABLE_FOR_CHAT:
		res = Presence.Mode.chat;
		break;
	    case CONTACT_STATUS_AWAY:
		res = Presence.Mode.away;
		break;
	    case CONTACT_STATUS_BUSY:
		res = Presence.Mode.dnd;
		break;
	    case CONTACT_STATUS_UNAVAILABLE:
		res = Presence.Mode.xa;
		break;
	    default:
		return null;
	}
	return res;
    }

    /**
     * Get the status of from a presence packet.
     * @param presence the presence containing status
     * @return an int representing the status
     */
    public static int getStatusFromPresence(final Presence presence) {
	int res = PresenceStatus.CONTACT_STATUS_DISCONNECT;
	if (presence.getType().equals(Presence.Type.unavailable)) {
	    res = PresenceStatus.CONTACT_STATUS_DISCONNECT;
	} else {
	    Mode mode = presence.getMode();
	    if (mode == null) {
		res = PresenceStatus.CONTACT_STATUS_AVAILABLE;
	    } else {
		switch (mode) {
		    case available:
			res = PresenceStatus.CONTACT_STATUS_AVAILABLE;
			break;
		    case away:
			res = PresenceStatus.CONTACT_STATUS_AWAY;
			break;
		    case chat:
			res = PresenceStatus.CONTACT_STATUS_AVAILABLE_FOR_CHAT;
			break;
		    case dnd:
			res = PresenceStatus.CONTACT_STATUS_BUSY;
			break;
		    case xa:
			res = PresenceStatus.CONTACT_STATUS_UNAVAILABLE;
			break;
		    default:
			res = PresenceStatus.CONTACT_STATUS_DISCONNECT;
			break;
		}
	    }
	}
	return res;
    }

    /**
     * Check if contact is online by his status.
     * @param status contact status
     * @return is online
     */
    public static boolean statusOnline(final int status) {
	return status != PresenceStatus.CONTACT_STATUS_DISCONNECT;
    }
    
    
    /**
     * Create a map which contains default status messages.
     * @param context The context of the roster adapter.
     * @return A Map<Integer, String> which assigns a status to a message.
     */
    @SuppressLint("UseSparseArrays")
	public static  Map<Integer, String> createDefaultStatusMessagesMap() {
	Map<Integer, String> defaultStatusMessages = new HashMap<Integer, String>();
	defaultStatusMessages.put(PresenceStatus.CONTACT_STATUS_AVAILABLE,"在线");
	defaultStatusMessages.put(PresenceStatus.CONTACT_STATUS_AVAILABLE_FOR_CHAT,"我有空聊天");
	defaultStatusMessages.put(PresenceStatus.CONTACT_STATUS_AWAY,"离开");
	defaultStatusMessages.put(PresenceStatus.CONTACT_STATUS_BUSY,"请勿打扰");
	defaultStatusMessages.put(PresenceStatus.CONTACT_STATUS_DISCONNECT,"离线");
	defaultStatusMessages.put(PresenceStatus.CONTACT_STATUS_UNAVAILABLE, "没有空");
	return defaultStatusMessages;
    }

}
